package com.sharkburner.aeris.ui.module.article.composition;

import java.io.Serializable;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sharkburner.aeris.bean.MessageResource;
import com.sharkburner.aeris.ui.module.article.component.ArticleGridDetailView;
import com.sharkburner.aeris.ui.module.article.component.ArticleGridListView;
import com.sharkburner.aeris.ui.module.article.model.ArticleModel;
import com.sharkburner.common.component.DetailViewButton;
import com.sharkburner.common.component.ListViewButton;
import com.sharkburner.common.listener.RepaintListener;
import com.vaadin.Application;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * 
 * @author inartea
 * 
 */
@Component
public class ArticleLayout extends VerticalLayout implements RepaintListener,
	Serializable {
    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected ArticleModel articleModel;

    @Autowired
    protected transient Application application;

    @Autowired
    protected transient MessageResource messageResource;

    @Autowired
    protected transient BeanFactory beanFactory;

    private HorizontalLayout topBar;
    private DetailViewButton detailViewButton;
    private ListViewButton listViewButton;

    private Table table;
    private ArticleCellRender cellRender;

    @PostConstruct
    public void init() {
	// setSizeFull();
	topBar = new HorizontalLayout();
	topBar.setHeight("30px");
	cellRender = new ArticleCellRender(
		beanFactory.getBean(ArticleGridListView.class));

	detailViewButton = new DetailViewButton();
	listViewButton = new ListViewButton();

	detailViewButton.addListener(new Button.ClickListener() {

	    @Override
	    public void buttonClick(ClickEvent event) {
		cellRender.changeView(beanFactory
			.getBean(ArticleGridDetailView.class));
		repaint();
	    }
	});

	listViewButton.addListener(new Button.ClickListener() {

	    @Override
	    public void buttonClick(ClickEvent event) {
		cellRender.changeView(beanFactory
			.getBean(ArticleGridListView.class));
		repaint();
	    }
	});

	topBar.addComponent(listViewButton);
	topBar.addComponent(detailViewButton);

	topBar.setComponentAlignment(listViewButton, Alignment.MIDDLE_RIGHT);
	topBar.setComponentAlignment(detailViewButton, Alignment.MIDDLE_RIGHT);

	table = new Table();
	table.setWidth("100.0%");
	table.setHeight("100.0%");
	table.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);

	initTable();

	addComponent(topBar);
	setComponentAlignment(topBar, Alignment.MIDDLE_RIGHT);

	addComponent(table);
    }

    private void initTable() {
	table.setSizeFull();

	table.addGeneratedColumn(ArticleModel.DESCR, cellRender);

	table.addListener(new RepaintRequestListener() {

	    @Override
	    public void repaintRequested(RepaintRequestEvent event) {
		application.getMainWindow().executeJavaScript(
			messageResource.getText("share.facebook.reinit"));
	    }
	});

	repaint();

	table.setColumnHeaders(new String[] { "DESCR" });
    }

    @Override
    public void repaint() {
	table.setContainerDataSource(articleModel.getData());
    }
}
